package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;
import static com.migu.contacts.util.DialogUtil.showVIPDialog;

import android.util.Log;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.BottomPopup;
import com.frank.libandroid.DataStore;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.Key;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.LabelBean;
import com.migu.contacts.databinding.ActivityLabelBinding;
import com.migu.contacts.databinding.ItemLabelBinding;
import com.migu.contacts.databinding.LabelAddBinding;
import com.migu.contacts.util.UserCenter;

import java.util.ArrayList;
import java.util.List;

public class LabelActivity extends BaseActivity<ActivityLabelBinding> {
    public static final Key<List<LabelBean>> key = new Key<>();

    @BindAdapter(R.id.topRecyclerView)
    BaseAdapter<ItemLabelBinding, LabelBean> topAdapter = new BaseAdapter<ItemLabelBinding, LabelBean>() {
        @OnClick(R.id.text)
        public void click(LabelBean bean, ItemLabelBinding binding) {
            if (binding.text.isSelected()) {
                selectedList.remove(bean);
            } else {
                selectedList.add(bean);
            }
            binding.text.setSelected(!binding.text.isSelected());
        }

        @Override
        protected void onBindData(ItemLabelBinding binding, LabelBean bean, int position) {
            super.onBindData(binding, bean, position);
            if (selectedList.contains(bean)) {
                binding.text.setSelected(true);
            } else {
                binding.text.setSelected(false);
            }
        }
    };
    @BindAdapter(R.id.bottomRecyclerView)
    BaseAdapter<ItemLabelBinding, LabelBean> bottomAdapter = new BaseAdapter<ItemLabelBinding, LabelBean>() {
        @OnClick(R.id.text)
        public void click(LabelBean bean, ItemLabelBinding binding) {
            if (binding.text.isSelected()) {
                selectedList.remove(bean);
            } else {
                selectedList.add(bean);
            }
            binding.text.setSelected(!binding.text.isSelected());
        }

        @Override
        protected void onBindData(ItemLabelBinding binding, LabelBean bean, int position) {
            super.onBindData(binding, bean, position);
            if (selectedList.contains(bean)) {
                binding.text.setSelected(true);
            } else {
                binding.text.setSelected(false);
            }
        }

    };
    List<LabelBean> selectedList = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        FlexboxLayoutManager layoutManager = (FlexboxLayoutManager) mBinding.topRecyclerView.getLayoutManager();
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager = (FlexboxLayoutManager) mBinding.bottomRecyclerView.getLayoutManager();
        layoutManager.setFlexDirection(FlexDirection.ROW);
    }

    @Override
    protected void initData() {
        super.initData();
        List<LabelBean> selected = DataStore.get(key);
        if (selected == null) {
            selected = new ArrayList<>();
        }
        List<LabelBean> finalSelected = selected;
        API.queryLabel().watch(this, new HttpCallback<List<LabelBean>>() {
            @Override
            protected void onSuccess(List<LabelBean> labelBeans) {
                Log.d(TAG, "onSuccess() called with: labelBeans = [" + labelBeans + "]");
                if (labelBeans == null) {
                    return;
                }
                List<LabelBean> topList = new ArrayList<>();
                List<LabelBean> bottomList = new ArrayList<>();
                for (LabelBean labelBean : labelBeans) {
                    if (labelBean.type == 0) {
                        topList.add(labelBean);
                    } else {
                        bottomList.add(labelBean);
                    }
                    for (LabelBean bean : finalSelected) {
                        if (bean.id == labelBean.id) {
                            selectedList.add(labelBean);
                            break;
                        }
                    }
                }
                topAdapter.setData(topList);
                bottomAdapter.addData(bottomList);
            }
        });
    }

    public void create(View view) {
        if (bottomAdapter.getData().size() >= 5 && !UserCenter.isVip()) {
            showVIPDialog(mActivity);
            return;
        }
        BottomPopup bottomPopup = new BottomPopup<LabelAddBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @Override
            protected void initData() {
                super.initData();
                mBinding.setBean(new LabelBean());
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                API.addLabel(mBinding.getBean()).watch(mActivity, new HttpCallback<LabelBean>() {
                    @Override
                    protected void onSuccess(LabelBean bean) {
                        Log.d(TAG, "onSuccess() called with: bean = [" + bean + "]");
                        bottomAdapter.getData().add(bean);
                        bottomAdapter.notifyDataSetChanged();
                        dismiss();
                    }
                });
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }


    public void save(View view) {
        DataStore.put(key, selectedList);
        setResult(RESULT_OK);
        finish();
    }
}